package com.dimong.www.loan.ui.adapter.holder;


import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.dimong.loan.sdk.ui.adapter.BaseRecyclerViewHolder;
import com.dimong.loan.sdk.utils.UIUtils;
import com.dimong.www.loan.R;
import com.dimong.www.loan.bean.FocusBean;
import com.dimong.loan.sdk.utils.GlideUtil;
import com.dimong.www.loan.utils.ViewUtils;
import com.dimong.www.loan.widget.AutoLoopViewPager;
import com.viewpagerindicator.CirclePageIndicator;

import java.util.ArrayList;
import java.util.List;

public class CarouselPicHolder extends BaseRecyclerViewHolder implements View.OnClickListener {
    private AutoLoopViewPager viewPager_item_carousel_pic;
    private CirclePageIndicator circle_item_carousel_pic;
    private CarouselPicHolderViewPagerAdapter carouselPicHolderViewPagerAdapter;
    private List<FocusBean> focusBeans;

    private CarouselPicHolderInterface carouselPicHolderInterface;

    public CarouselPicHolder(Context context, ViewGroup parent, CarouselPicHolderInterface carouselPicHolderInterface) {
        super(context, R.layout.item_carousel_pic, parent);

        viewPager_item_carousel_pic = itemView.findViewById(R.id.viewPager_item_carousel_pic);
        ViewUtils.setViewParams(viewPager_item_carousel_pic, UIUtils.getWidth(), UIUtils.getWidth() * 9 / 16);
        circle_item_carousel_pic = itemView.findViewById(R.id.circle_item_carousel_pic);

        carouselPicHolderViewPagerAdapter = new CarouselPicHolderViewPagerAdapter();
        viewPager_item_carousel_pic.setAdapter(carouselPicHolderViewPagerAdapter);
        focusBeans = new ArrayList<>();
        circle_item_carousel_pic.setViewPager(viewPager_item_carousel_pic);

        this.carouselPicHolderInterface = carouselPicHolderInterface;
    }

    @Override
    public void setData(Object object) {
        super.setData(object);
        if (object instanceof List) {
            List<FocusBean> focus = (List<FocusBean>) object;
            focusBeans.clear();
            for (int i = 0; i < focus.size(); i++) {
                focusBeans.add(focus.get(i));
            }
            viewPager_item_carousel_pic.onResume();
            carouselPicHolderViewPagerAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_focus_bean) {
            if (carouselPicHolderInterface != null) {
                carouselPicHolderInterface.onCarouselPicHolderClick(getLayoutPosition(), (Integer) v.getTag());
            }
        }
    }

    public class CarouselPicHolderViewPagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            if (focusBeans != null) {
                return focusBeans.size();
            } else {
                return 0;
            }
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View mContentView = LayoutInflater.from(mContext).inflate(R.layout.frag_carousel_pic, null);
            ImageView iv_focus_bean = mContentView.findViewById(R.id.iv_focus_bean);
            FocusBean focusBean = focusBeans.get(position);
            GlideUtil.loadImg(mContext, focusBean.getFocus_logo(), iv_focus_bean);
            iv_focus_bean.setTag(position);
            iv_focus_bean.setOnClickListener(CarouselPicHolder.this);
            ViewUtils.setViewParams(iv_focus_bean, UIUtils.getWidth(), UIUtils.getWidth() * 9 / 16);
            container.addView(mContentView);
            return mContentView;
        }
    }

    public interface CarouselPicHolderInterface {
        void onCarouselPicHolderClick(int adapterPos, int pos);
    }
}
